<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('Controller', 'Controller');
App::uses('Mobile_Detect', 'Vendor/mobiledetect');
App::uses('CakeEmail', 'Network/Email');
/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
   
    public $components = array(
            'Session',
            'Cookie' 
    );
    
    public $helpers = array('Html');

    
    public function beforeFilter(){
		parent::beforeFilter();
        //$this->_setErrorLayout();
        
        $lang_code_default = Configure::read('lang_code_default');
        if($this->Session->read('lang_code')){
            $lang_code = $this->Session->read('lang_code');
        }
        else{
            $lang_code = $lang_code_default;
            $this->Session->write('lang_code', $lang_code_default);
        }
        $this->set('lang_code', $lang_code);
        
        $this->loadModel('Setting'); $this->Setting->lang_code = $lang_code;
        $this->loadModel('Menu'); $this->Menu->lang_code = $lang_code;
        
        $settings = $this->Setting->get_setting();
        $this->set('settings', $settings);
        
        
        $info_user = array();
        if($this->Session->read('Auth.User'))
            $info_user = $this->Session->read('Auth.User');
        $this->set('info_user', $info_user);
        
        
        $main_menu = $this->Menu->get_menu_by_code('main_menu');
        $this->set('main_menu', $main_menu);
        
        
        /*
        $this->Cookie->name = 'cutkit';
        $this->Cookie->time = 3600;  // or '1 hour'
        $this->Cookie->path = '/';
        $this->Cookie->domain = 'http://localhost';
        $this->Cookie->secure = true;  // i.e. only sent if using secure HTTPS
        $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
        $this->Cookie->httpOnly = true;
        $this->Cookie->type('aes');
        */
        
            
        $is_tablet = false;
		$is_mobile = $this->request->is('mobile');
        $MobileDetect = new Mobile_Detect();
		if ($is_mobile) {
			$is_tablet = $MobileDetect->isTablet();
		}
        else{
            $is_mobile = $MobileDetect->isMobile();
        }
		$this->set('is_mobile', $is_mobile);
        $this->is_mobile = $is_mobile;   
        
        if($this->is_mobile)
        {
            if($this->Cookie->read('redirect_to_product')){
                $this->Cookie->write('redirect_to_product','1', false, 1800);
            }
            else
            {
                $this->Cookie->write('redirect_to_product','1', false, 1800);
                return $this->redirect(
                    array('controller' => 'products', 'action' => 'index')
                );
            }
        }
	}
    

    
    function _setErrorLayout() {  
     if ($this->name == 'CakeError') {  
        //$this->layout = 'eror';  
        $this->redirect(array('controller' => 'errors', 'action' => 'error404'));
     }    
}  
}
